package com.lxc.crud.mapper;

import com.lxc.crud.entity.User;
import org.apache.ibatis.jdbc.SQL;

public class UserMapperSql {

    public String selectByMultiCondition(User user){
        return new SQL(){{
            SELECT("*");
            FROM("user");
            if(user.getId() != null){
                WHERE("id=#{id}");
            }
            if(user.getName() != null){
                WHERE("name=#{name}");
            }
            if(user.getAge() != null){
                WHERE("age=#{age}");
            }
            if(user.getEmail() != null){
                WHERE("email=#{email}");
            }
            if(user.getAddress() != null){
                WHERE("address=#{address}");
            }
        }}.toString();
    }

    public String updateById(User user){
        return new SQL(){{
            UPDATE("user");
            if(user.getName() != null){
                SET("name=#{name}");
            }
            if(user.getAge() != null){
                SET("age=#{age}");
            }
            if(user.getEmail() != null){
                SET("email=#{email}");
            }
            if(user.getAddress() != null){
                SET("address=#{address}");
            }
            WHERE("id=#{id}");
        }}.toString();
    }
}


